<?php
/**
 * Created by PhpStorm.
 * User: spring
 * Date: 18-12-12
 * Time: 上午10:02
 */

namespace App\Http\Controllers\Adminer;
use App\Http\Controllers\AdminController as adminc;
use App\Models\Admin;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
class AdminController extends adminc
{
    protected $data;
    function __construct()
    {
        parent::__construct();
        $this->data['title'] = $this->base['controller'].$this->base['action'];
        $this->data['title_en'] = "Admin";
        $this->data['title_url'] = route("admin.index");
        $this->data['is_search'] = TRUE;
    }

    public function index(Request $request){
        if(!empty($request->get('keyword'))){
            $keyword = $request->get('keyword');
            $where[] = array("username","like",'%'.$keyword.'%');
            //$this->data['keyword'] = $keyword;
            session()->put('keyword',$keyword);
        }
        $where[] = array("role","!=",0);
        $admins = Admin::where($where)->orderBy('id','asc')->Paginate(15);
        $this->data['list'] = $admins;
        return view("adminer.admin.index",$this->data);
    }

    public function create(){
        $this->data['roles'] = Role::where('status',Role::STATUS_SUCCESS)->orderBY('lft','asc')->get();
        return view("adminer.admin.create",$this->data);
    }

    public function store(Request $request){
        $info=$request->only(['username', 'password','password_confirmation','role','status','email','realname','sex','phone','qq','address']);
        // 验证不为空
        Validator::make($info, [
            'username' => ['bail','required',Rule::unique('admin'),],
            'email' => ['bail','required'],
            'realname' => ['bail','required'],
            'phone' => ['bail','required'],
            'password' => ['bail','required','confirmed'],
        ],[
            'username.required'       => '请输入用户名',
            'username.unique'         => '用户名已使用',
            'email.required'       => '请输入邮箱',
            'realname.required'       => '请输入真实姓名',
            'phone.required'       => '请输入电话',
            'password.required'       => '请输入密码',
            'password.confirmed'         => '两次输入的密码不一致',
        ])->validate();
        $data = new Admin($info);
        $data->password = Hash::make($info['password']);
        $data->create_ip = $request->getClientIp();
        if(!$data->save()){
            AdminLogController::writeLog($_SERVER["QUERY_STRING"],json_encode($_POST),"新增用户失败");
            throw new BaseException('保存失败');
        }
        AdminLogController::writeLog($_SERVER["QUERY_STRING"],json_encode($_POST),"新增用户－".$info['username']);
        return $this->successResponse('保存成功',[],200,route("admin.index"));
    }

    public function edit($id,Request $request){
        $this->data['info'] = Admin::find($id);
        $this->data['roles'] = Role::where('status',Role::STATUS_SUCCESS)->orderBY('lft','asc')->get();
        //dd($this->data);
        return view("adminer.admin.edit",$this->data);
    }

    public function update($id,Request $request){
        $info = Admin::find($id);
        if($info){
            $payload=$request->only(['password','password_confirmation','role','status','email','realname','sex','phone','qq','address']);
            // 验证不为空
            Validator::make($payload, [
                //'username' => ['bail','required',Rule::unique('admin')->ignore($id)],
                'email' => ['bail','required'],
                'realname' => ['bail','required'],
                'phone' => ['bail','required'],
                'password' => ['bail','required','confirmed'],
            ],[
                //'username.required'       => '请输入用户名',
                //'username.unique'         => '用户名已使用',
                'email.required'       => '请输入邮箱',
                'realname.required'       => '请输入真实姓名',
                'phone.required'       => '请输入电话',
                'password.required'       => '请输入密码',
                'password.confirmed'         => '两次输入的密码不一致',
            ])->validate();
            $payload['password'] = Hash::make($payload['password']);
            if(!$info->update($payload)){
                AdminLogController::writeLog($_SERVER["QUERY_STRING"],json_encode($_POST),"修改用户失败");
                throw new BaseException('保存失败');
            }
            AdminLogController::writeLog($_SERVER["QUERY_STRING"],json_encode($_POST),"修改用户-".$payload['username']);
            return $this->successResponse('保存成功',[],200,route("admin.index"));
        }else{
            AdminLogController::writeLog($_SERVER["QUERY_STRING"],$id,"数据错误");
            return response()->json([
                'code'      => 422001,
                'message'   => "数据错误",
            ]);
        }
    }

    public function destroy($id){
        if(!Admin::destroy($id)){
            throw new BaseException('操作失败');
        }
        return $this->successResponse('操作成功');
    }
}